/**
 * @(#)CodeSerivceImpl.java
 * 版权声明() 厦门工程有限公司, 版权所有 违者必究
 *
 * 修订记录:
 * 1)创建者:claude
 *   时间:2007-8-7-下午08:00:15
 */
package com.devframe.sysconfig.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.devframe.sys.util.assertion.Assert;
import com.devframe.sys.util.collection.MyHashMap;
import com.devframe.sysbase.service.BaseService;
import com.devframe.sysconfig.service.CodeService;
import com.devframe.sysconfig.storage.operate.WriterCacheData;

/**
 * 
 * <pre>
 * &lt;b&gt;&lt;font color=&quot;blue&quot;&gt;CodeServiceImpl&lt;/font&gt;&lt;/b&gt;
 * </pre>
 * 
 * <pre>
 * &lt;b&gt; --代码表缓存处理--&lt;/b&gt;
 * </pre>
 * <pre>
 *   初始化时只加载需要缓存的表名列表，只在需要时加载实际数据。
 *   代码表统一用HashMap模型实现数据表示，统一字段如下
 *   CODE 代码
 *   NAME 名称
 *   
 *   主要接口：
 *      方便用于代码名称翻译，提供代码名称映射。
 *      提供代码到名称翻译接口。
 *      提供重新初始化接口。
 *      
 * &lt;pre&gt;
 * &lt;b&gt;--样例--&lt;/b&gt;
 *   CodeServiceImpl obj = new CodeServiceImpl();
 *   obj.method();
 * &lt;/pre&gt;
 * JDK版本：JDK1.4
 * &#064;author  &lt;b&gt;&lt;/b&gt;
 * 
 */
@Service(value="codeService")
public class CodeServiceImpl extends BaseService implements CodeService {

    /**
     * LOG
     */
    private Logger logger = Logger.getLogger(CodeServiceImpl.class);

    /**
     * 数据缓存写入器
     */
    @Autowired
    private WriterCacheData writerCacheData;

	/**
     * loadBMData 加载代码表
     * 形成代码名称翻译Map
     * @param codeTable
     *            initLoadTable记录
     * @return Map
     * 
     */
    public  Map loadCodeTableData(Map codeTable) {
    	Map<String,String> myCodeTable=codeTable;
    	
        StringBuffer sql = new StringBuffer();
        String bhField = "CODE";
        String mcField = "NAME";
        String sortField = "CODE";

        sql.append("SELECT ");
        if (StringUtils.isNotEmpty((String) myCodeTable.get("CODE_FIELD"))) {
            bhField = (String) codeTable.get("CODE_FIELD");
            sql.append(bhField).append(" CODE,");
        }
        if (StringUtils.isNotEmpty((String) myCodeTable.get("NAME_FIELD"))) {
            mcField = (String) codeTable.get("NAME_FIELD");
            sql.append(mcField).append(" NAME ");
        }
        if (StringUtils.isNotEmpty((String) myCodeTable.get("SORT_FIELD"))) {
            sortField = (String) codeTable.get("SORT_FIELD");
        }
        sql.append(" FROM " + myCodeTable.get("TABLE_NAME") + " ORDER BY " + sortField);
        List list =super.getBaseDao().sqlQueryForList(sql.toString());
        Map codeMap = new MyHashMap();
        if (list != null) {
            int len = list.size();
            for (int i = 0; i < len; i++) {
                Map codeRow = (Map) list.get(i);
                codeMap.put(codeRow.get("CODE"), codeRow.get("NAME"));
            }
        }
        return codeMap;
    }

    /**
     * 
     * 
     */
    public void init() {

        logger.info("开始初始化代码表缓存...");
        //清空数据
        this.writerCacheData.clearCodeTableData();
        
        String sql = "SELECT TABLE_ALIAS,TABLE_NAME,MEMO,CODE_FIELD,NAME_FIELD,SORT_FIELD " +
        		" FROM T_INIT_LOAD_TABLE A WHERE A.TABLE_TYPE='1' ";
        List tableList = super.getBaseDao().sqlQueryForList(sql);
        if (tableList != null) {
            int len = tableList.size();
            Map table;
            String tableName;
            for (int i = 0; i < len; i++) {
                table= (Map) tableList.get(i);
                //转为大写
                tableName=table.get("TABLE_ALIAS").toString().trim().toUpperCase();
                this.writerCacheData.addCodeTableSet(tableName, table);
            }
            logger.info("共配置了" + len + "个代码表.");
        } else {
            logger.info("共配置了" + 0 + "个代码表.");
        }
    }



	public void setWriterCacheData(WriterCacheData writerCacheData) {
		this.writerCacheData = writerCacheData;
		this.writerCacheData.bindCodeServiceHandle(this);
	}



 

}
